﻿#include "park.h"
#include <stdlib.h>

int capacity;
int price_per_hour;

Car* new_car(void)
{
	Car* p_car;
	char name[PLATE_SIZE];

	printf("车牌号：\n");
	scanf_s("%s", name,PLATE_SIZE);
	time_t time;
	do
	{
		time = parse_stdin();
	} while (time == (time_t)-1);

	ASSERT_ABORT(p_car = calloc(1, sizeof(Car)), "内存分配失败");
	memcpy(p_car->name, name, sizeof(name));
	p_car->startTime = time;
	p_car->next = NULL;
	return p_car;
}

Car* leave(char* name) {
	Car* temp = NULL, * ret = NULL;
	time_t time_temp;
	while (1) {
		temp = pop(&parking_lot);
		if (temp == NULL)
		{
			fprintf((__acrt_iob_func(2)), "%s\n", ("没有该车辆"));
			system("pause");
			break;
		}
		ret = temp;
		if (strcmp(name, temp->name) == 0) {
			printf("车%s已离开\n", name);
			break;
		}
		printf("%s 进入临时停车场...\n",temp->name);
		if (!push(&temp_parking_lot, temp))
		{
			printf("进入失败\n");
			break;
		}
	}
	while (1) {
		temp = pop(&temp_parking_lot);
		if (temp == NULL) 
		//all the cars have left back to park
		{
			printf("组织临时停车场迁回完毕\n");
			break;
		}
		printf("%s 出临时停车场...\n", temp->name);
		if (!push(&parking_lot, temp)){
			fprintf(stderr,"组织临时停车场迁回时发生未知错误\n");
			return NULL;
		}
	}
	Car* p = ret;
	printf("请输入离开时间:\n");
	do
	{
		time_temp = parse_stdin();
		ASSERT_RETRY(
			time_temp != -1 && time_temp > p->startTime,
			"无效时间\n", 0);
	} while (0);
	timeParked(time_temp - p->startTime);
	Car*  toEnter = deque();
	if (toEnter) {
		toEnter->startTime = time_temp;
	}
	push(&parking_lot,toEnter);
	return ret;
}
